using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

	protected Transform player;
    protected bool attackState;

	public float speed;
	public int hp;
	public int damage;
    protected float damageTimer;
    protected float damageDelay;

	public virtual void Start () 
	{
		player = GameObject.FindGameObjectWithTag("Player").transform;
	}

    public virtual void Update() 
	{
		if(!attackState && CharMovement.playing == true && hp > 0)
		{
            Attack();
		}
	}

    public virtual void Attack() 
    {

    }
	
	void OnCollisionEnter (Collision player)
	{
		if(player.gameObject.tag == "Player")
		{
            attackState = true;
			CharMovement c = player.gameObject.GetComponent<CharMovement>();
			c.removeHP(damage);
		}
	}

    void OnCollisionStay(Collision player)
    {
        CharMovement c = player.gameObject.GetComponent<CharMovement>();

        if (player.gameObject.tag == "Player")
        {
            damageTimer += Time.deltaTime;

            if (damageTimer > damageDelay)
            {
                c.removeHP(damage);
                damageTimer = 0;
            }
        }
    }
	
	void OnCollisionExit (Collision player)
	{
		StartCoroutine(reactiveAI());
	}

	public void removeHP (int damage)
	{
		this.hp -= damage;
		
		if(this.hp <= 0)
		{
			tag = "Untagged";
            player.gameObject.SendMessage("AddedKill");

            Destroy (gameObject);
		}
	}	
	
	IEnumerator reactiveAI()
	{
		yield return new WaitForSeconds(0.5f);
		attackState = false;
	}
}
